package com.example.wangzh.developkit.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.wangzh.developkit.R;

/**
 * Created by USER on 2017/1/9.
 */

public class ViewUtils {
    public static Dialog dialog;

    public static void visibleOrInvisible(View view, boolean visible) {
        if (visible) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.INVISIBLE);
        }
    }

    public static void visibleOrGone(View view, boolean visible) {
        if (visible) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.setVisibility(View.GONE);
        }
    }

    public static void setVisibility(View view, int visibility) {
        switch (visibility) {
            case View.VISIBLE:
                view.setVisibility(View.VISIBLE);
                break;
            case View.INVISIBLE:
                view.setVisibility(View.INVISIBLE);
                break;
            case View.GONE:
                view.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    public static void switchView(View viewShow, View viewHide) {
        viewShow.setVisibility(View.VISIBLE);
        viewHide.setVisibility(View.GONE);
    }

    /**
     * 这里面设定有些不合理，有的时候计算Item的高度用的是最小高度而不是实际高度
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        if (listView == null)
            return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }
        int totalHeight = 0;
        totalHeight += listView.getPaddingBottom() + listView.getPaddingTop();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

    public static void showApplicationDialog(Context context) {
        if (dialog == null || !dialog.isShowing()) {
            dialog = getCenterProgressDialog(context, "载入中...", true);
            dialog.show();
        }
    }

    public static void dismissApplicationDialog() {
        if (dialog != null && dialog.isShowing()) {
            try {
                dialog.dismiss();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 自定义居中进度条
     *
     * @param context
     * @param context
     * @param text      文本内容
     * @param isShowTxt 是否显示文本
     * @return
     */
    public static Dialog getCenterProgressDialog(Context context, CharSequence text, boolean isShowTxt) {

        final Dialog dialog = new Dialog(context, R.style.DialogStyle);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.custom_progress_dialog);
        Window window = dialog.getWindow();
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        WindowManager.LayoutParams lp = window.getAttributes();
        TextView titleTxtv = (TextView) dialog.findViewById(R.id.dialogText);
        titleTxtv.setText(text);
        if (isShowTxt) {
            titleTxtv.setVisibility(View.VISIBLE);

            lp.width = UIUtils.dip2Px(140);
            lp.height = UIUtils.dip2Px(140);
        } else {

            titleTxtv.setVisibility(View.GONE);

            lp.width = UIUtils.dip2Px(90);
            lp.height = UIUtils.dip2Px(90);
        }
        return dialog;
    }

}
